package co.com.smart.eagle.client.activity;

import java.util.List;

import co.com.smart.eagle.client.EagleCallBack;
import co.com.smart.eagle.client.factory.EagleFactory;
import co.com.smart.eagle.client.rpcservices.login.LoginService;
import co.com.smart.eagle.client.rpcservices.login.LoginServiceAsync;
import co.com.smart.eagle.client.view.CrearRolesView;
import co.com.smart.eagle.client.view.CrearRolesView.Presenter;
import co.com.smart.eagle.client.view.impl.CrearRolesViewImpl.FocusRolPlace;
import co.com.smart.eagle.client.view.impl.components.SmartAlertDialog.CallBackModal;
import co.com.smart.eagle.client.view.util.SmartLoading;
import co.com.smart.eagle.client.view.util.SmartMensajes;
import co.com.smart.eagle.infrastructure.dto.RolesDTO;
import co.com.smart.eagle.infrastructure.dto.SmartTipoMensajes;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class CrearRolesActivity extends AbstractActivity implements Presenter {

	private CrearRolesView view;

	private LoginServiceAsync service = GWT.create(LoginService.class);

	public CrearRolesActivity(EagleFactory eagleFactory) {
		view = eagleFactory.getCrearRolesView();
		view.setPresenter(this);
		view.cleanForm();
		cargarRoles();
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		panel.setWidget(view);
	}

	@Override
	public void crearRol() {
		EagleCallBack<Void> callBack = new EagleCallBack<Void>(true) {
			@Override
			public void success(Void result) {

				SmartMensajes.getInstance().show("Informaci\u00F3n",
						"Rol almacenado correctamente",
						SmartTipoMensajes.INFORMACION, new CallBackModal() {

							@Override
							public void finModal() {
								view.cleanForm();
								cargarRoles();
								view.setFocus(FocusRolPlace.ROL);
							}
						});

			}
		};
		SmartLoading.getInstance().show();
		RolesDTO rol = new RolesDTO();
		rol.setDsRol(view.getRolName());
		service.setRol(rol, callBack);
	}

	@Override
	public void cargarRoles() {
		EagleCallBack<List<RolesDTO>> callBack = new EagleCallBack<List<RolesDTO>>(
				true) {
			@Override
			public void success(List<RolesDTO> result) {
				view.setRoles(result);
			}
		};
		SmartLoading.getInstance().show();
		service.getRoles(callBack);
	}

	@Override
	public boolean validateCreateRol() {
		if ("".equals(view.getRolName())) {

			SmartMensajes.getInstance().show("Validaci\u00F3n",
					"Ingrese el rol", SmartTipoMensajes.ADVERTENCIA,
					new CallBackModal() {

						@Override
						public void finModal() {
							view.setFocus(FocusRolPlace.ROL);
						}
					});

			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@Override
	public void editRol(RolesDTO rol) {
	}

	@Override
	public void deleteRol(RolesDTO rol) {
		EagleCallBack<Void> callBack = new EagleCallBack<Void>(true) {
			@Override
			public void success(Void result) {
				SmartMensajes.getInstance().show("Informaci\u00F3n",
						"Rol eliminado correctamente",
						SmartTipoMensajes.INFORMACION, new CallBackModal() {

							@Override
							public void finModal() {
								view.cleanForm();
								cargarRoles();
								view.setFocus(FocusRolPlace.ROL);
							}
						});

			}
		};
		SmartLoading.getInstance().show();
		service.borrarRol(rol, callBack);
	}

}
